package org.xinghai.sql.main.generator.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xinghai.sql.main.dto.CreateColumnDto;
import org.xinghai.sql.main.dto.UpdateColumnDto;
import org.xinghai.sql.main.generator.domain.DatabaseTables;
import org.xinghai.sql.main.generator.domain.TableColumns;
import org.xinghai.sql.main.generator.mapper.DatabaseTablesMapper;
import org.xinghai.sql.main.generator.mapper.TableColumnsMapper;
import org.xinghai.sql.main.generator.service.TableColumnsService;
import org.xinghai.sql.main.utils.TypeMapperUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author lijia
* &#064;description  针对表【table_columns】的数据库操作Service实现
* &#064;createDate  2025-07-28 11:45:59
*/
@Service
public class TableColumnsServiceImpl extends ServiceImpl<TableColumnsMapper, TableColumns>
    implements TableColumnsService {
    @Autowired
    private DatabaseTablesMapper databaseTablesMapper;

    @Override
    public List<TableColumns> getList(List<DatabaseTables> databaseTables) {
        //遍历databaseTables，获取tableId
        List<Integer> tableIds = databaseTables.stream().map(DatabaseTables::getTableId).collect(Collectors.toList());
        //根据tableId查询tableColumns
        return baseMapper.selectList(new LambdaQueryWrapper<TableColumns>().in(TableColumns::getTableId, tableIds));
    }
    //从表名和列名查询列id
    @Override
    public Integer getColumnIdByColumnName(String tableName, String columnName) {
        return baseMapper.getColumnIdFromTableNameAndColumnName(tableName, columnName);
    }
    //根据目标表id还有目标目标列名得到目标列id
    @Override
    public Integer getColumnIdByColumnNameAndForeignTableId(Integer foreignTableId, String foreignColumnName) {
        return baseMapper.getColumnIdFromTableNameAndColumnNameAndForeignTableId(foreignTableId, foreignColumnName);
    }
    //新增列
    @Override
    public void createColumn(CreateColumnDto createColumnDto) {

        String dataType = createColumnDto.getDataType();
        //将数据库数据类型转换成java映射类型
        String javaType = String.valueOf(TypeMapperUtils.sqlTypeToJavaType(dataType));
        //根据表名与表所属数据库查询表id
        Integer tableId = databaseTablesMapper.
                getTableIdByTableNameFromDatabaseName
                        (createColumnDto.getTableName(),createColumnDto.getDatabaseName());
        //如果表不存在，抛出异常
        if(tableId == null){
            throw new RuntimeException("表不存在");
        }
        TableColumns tableColumns = new TableColumns();

        tableColumns.setTableId(tableId);
        //设置列描述
        tableColumns.setDescription(createColumnDto.getDescription());
        //设置外键描述
        tableColumns.setForeignKeyDescription(createColumnDto.getForeignKeyDescription());

        tableColumns.setTableName(createColumnDto.getTableName());
        tableColumns.setColumnName(createColumnDto.getColumnName());
        tableColumns.setDataType(createColumnDto.getDataType());
        tableColumns.setIsNullable(createColumnDto.getIsNullable());
        tableColumns.setIsPrimaryKey(createColumnDto.getIsPrimaryKey());
        tableColumns.setIsForeignKey(createColumnDto.getIsForeignKey());
        //将数据库数据类型转换成java映射类型
        tableColumns.setJavaType(javaType);

        tableColumns.setForeignTableName(createColumnDto.getForeignTableName());
        tableColumns.setForeignColumnName(createColumnDto.getForeignColumnName());

        try {
            //如果是外键，查询目标表ID
            if(createColumnDto.getIsForeignKey() == 1){
                Integer foreignTableId = databaseTablesMapper.getTableIdByTableNameFromDatabaseName
                        (createColumnDto.getForeignTableName(),createColumnDto.getDatabaseName());
                tableColumns.setForeignTableId(foreignTableId);
            }
            //查询目标列ID
            if(createColumnDto.getIsForeignKey() == 1){
                Integer foreignColumnId = getColumnIdByColumnNameAndForeignTableId
                        (tableColumns.getForeignTableId(),createColumnDto.getForeignColumnName());
                tableColumns.setForeignColumnId(foreignColumnId);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        tableColumns.setDescription(createColumnDto.getDescription());
        tableColumns.setColumnPosition(createColumnDto.getColumnPosition());
        baseMapper.insert(tableColumns);
    }
   //更新列，用列id确定需要更新的列
    @Override
    public void updateColumn(UpdateColumnDto updateColumnDto) {
        TableColumns tableColumns = new TableColumns();
        //设置列id
        tableColumns.setColumnId(updateColumnDto.getColumnId());
        //设置列描述
        tableColumns.setDescription(updateColumnDto.getDescription());
        //设置列位置
        tableColumns.setColumnPosition(updateColumnDto.getColumnPosition());
        //设置列名
        tableColumns.setColumnName(updateColumnDto.getColumnName());
        //设置数据类型
        tableColumns.setDataType(updateColumnDto.getDataType());
        //设置java类型
        String javaType = String.valueOf(TypeMapperUtils.sqlTypeToJavaType(updateColumnDto.getDataType()));
        tableColumns.setJavaType(javaType);
        //设置是否为空
        tableColumns.setIsNullable(updateColumnDto.getIsNullable());
        //设置是否主键
        tableColumns.setIsPrimaryKey(updateColumnDto.getIsPrimaryKey());
        //设置是否外键
        tableColumns.setIsForeignKey(updateColumnDto.getIsForeignKey());
        //设置外键目标表名
        tableColumns.setForeignTableName(updateColumnDto.getForeignTableName());
        //设置外键目标列名
        tableColumns.setForeignColumnName(updateColumnDto.getForeignColumnName());

        baseMapper.updateById(tableColumns);
    }

    @Override
    public void removeByDatabaseName(String databaseName) {
        baseMapper.removeByDatabaseName(databaseName);
    }
}




